﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Webnet
{
    public class HTMLTools
    {
        public int GetHtmlWidth(string htmlCode, int startIndex)
        {
            htmlCode = htmlCode.Replace("\r\n", "");
            htmlCode = htmlCode.Replace("\r", "");
            string widthKeywordStart = @" width=""";
            string widthKeywordEnd = @"""";
            int lastIndex = htmlCode.LastIndexOf(widthKeywordStart);
            if (lastIndex == -1)
            {
                widthKeywordStart = @"&amp;w=";
                widthKeywordEnd = "&";

                lastIndex = htmlCode.LastIndexOf(widthKeywordStart);
                if (lastIndex == -1)
                    return -1;
            }
            int widthCurrentStartIndex = 0;
            string widthString = string.Empty;
            do
            {
                widthCurrentStartIndex = htmlCode.IndexOf(widthKeywordStart, widthCurrentStartIndex) + widthKeywordStart.Length;
                int widthEndIndex = htmlCode.IndexOf(widthKeywordEnd, widthCurrentStartIndex);
                widthString = htmlCode.Substring(widthCurrentStartIndex, widthEndIndex - widthCurrentStartIndex);
            } while (widthString.Contains("%") || widthCurrentStartIndex == lastIndex);

            if (widthString.Contains("%"))
                return -1;
            else
            {
                widthString = widthString.ToLower().Replace("px", "");
                return Convert.ToInt32(widthString);
            }
        }

        public int GetHtmlHeight(string htmlCode, int startIndex)
        {
            htmlCode = htmlCode.Replace("\r\n", "");
            htmlCode = htmlCode.Replace("\r", "");
            string heightKeywordStart = @" height=""";
            string heightKeywordEnd = @"""";
            int lastIndex = htmlCode.LastIndexOf(heightKeywordStart);
            if (lastIndex == -1)
            {
                heightKeywordStart = @"&amp;h=";
                heightKeywordEnd = "&";

                lastIndex = htmlCode.LastIndexOf(heightKeywordStart);
                if (lastIndex == -1)
                    return -1;
            }

            int heightCurrentStartIndex = 0;
            string heightString = string.Empty;
            do
            {
                heightCurrentStartIndex = htmlCode.IndexOf(heightKeywordStart, heightCurrentStartIndex) + heightKeywordStart.Length;
                int heightEndIndex = htmlCode.IndexOf(heightKeywordEnd, heightCurrentStartIndex);
                heightString = htmlCode.Substring(heightCurrentStartIndex, heightEndIndex - heightCurrentStartIndex);
            } while (heightString.Contains("%") || heightCurrentStartIndex == lastIndex);

            if (heightString.Contains("%"))
                return -1;
            else
            {
                heightString = heightString.ToLower().Replace("px", "");
                return Convert.ToInt32(heightString);
            }
        }
    }
}
